package com.kangyi.admin.controller;

import com.kangyi.admin.service.FeeService;
import com.kangyi.dto.AddFeeDTO;
import com.kangyi.dto.FeeDTO;
import com.kangyi.dto.FeeQueryDTO;
import com.kangyi.result.PageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.FeeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/fee")
@Slf4j
@Api(tags = "养老院-费用相关接口")
public class FeeController {

    @Autowired
    private FeeService feeService;

    @GetMapping("/page")
    @ApiOperation("查询养老院下的费用")
    public Result<PageResult> pageQueryFee(FeeQueryDTO feeQueryDTO){
        log.info("查询此养老院下的费用:{}",feeQueryDTO);
        try {
            PageResult pageResult = feeService.pageQuery(feeQueryDTO);
            return Result.success(pageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("新增收费")
    @CacheEvict(cacheNames = "fee_", allEntries = true)
    public Result addFee(@RequestBody AddFeeDTO addFeeDTO){
        log.info("新增收费：{}", addFeeDTO);
        try {
            feeService.addFee(addFeeDTO);
            return Result.success("新增成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除收费")
    @CacheEvict(cacheNames = "fee_", allEntries = true)
    public Result deleteFee(@RequestParam List<Integer> ids){
        log.info("批量删除收费:{}",ids);
        try {
            feeService.deleteFee(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询费用")
//    @Cacheable(cacheNames = "fee_", key = "'adminGet_'+#id")
    public Result<FeeVO> getFee(@PathVariable Integer id){
        log.info("根据id查询费用：{}",id);
        try {
            FeeVO feeVO = feeService.getFee(id);
            return Result.success(feeVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/updateFee")
    @ApiOperation("修改入住收费详情")
    @CacheEvict(cacheNames = "fee_", allEntries = true)
    public Result updateFee(@RequestBody FeeDTO FeeDTO){
        log.info("修改入住收费详情:{}", FeeDTO);
        try {
            feeService.updateFee(FeeDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
